package com.itchu.system.controller;

import com.itchu.common.domain.PageResult;
import com.itchu.system.domain.User;
import com.itchu.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查找用户的分页信息
     * @param key   搜索条件
     * @param page  当前页
     * @param rows  页大小
     * @param sortBy
     * @param desc
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageResult<User>> findByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", required = false) Boolean desc
    ) {
        PageResult<User> list = userService.findByPage(key,page,rows,sortBy,desc);
        return ResponseEntity.ok(list);
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping("/save")
    public ResponseEntity<User> updateUser(User user) {

        User u = userService.updateUser(user);
        return ResponseEntity.ok(u);
    }

    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public ResponseEntity<Void> deletUser(
            @RequestParam("id")Long id) {
        userService.deletUser(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

//    /**
//     * 检查用户名或者电话是否已被注册
//     * @param type  1是用户名， 2是手机
//     * @param value  用户名或者手机号
//     * @return
//     */
//    @GetMapping("/register/checkOnly")
//    public ResponseEntity<Boolean> checkNameAndPhone(
//            @RequestParam(value = "type") Long type,
//            @RequestParam(value = "value") String value
//    ) {
//        Boolean b = userService.checkNameAndPhone(type, value);
//        return ResponseEntity.ok(b);
//    }

//    /**
//     * 向阿里云发送验证码操作
//     * @return
//     */
//    @PostMapping("/code")
//    public ResponseEntity<Void> sendCode(@RequestParam("phone") String phone) {
//        userService.sendCode(phone);
//        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
//    }

//    /**
//     * 注册业务
//     * @param user  用户的信息
//     * @param code  验证码
//     * @return
//     */
//    @PostMapping("/register")
//    public ResponseEntity<Boolean> register(User user ,@RequestParam("code") String code) {
//
//        Boolean b = userService.register(user,code);
//
//        return ResponseEntity.ok(b);
//    }

}
